package com.lcn.interceptors;

import com.lcn.base.BaseInfoProperties;
import com.lcn.exceptions.GraceException;
import com.lcn.grace.result.ResponseStatusEnum;
import com.lcn.utils.IPUtil;
import com.lcn.utils.MD5Utils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 短信拦截器
 */
@Slf4j
public class SmsInterceptor extends BaseInfoProperties implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ip = MD5Utils.encrypt(IPUtil.getRequestIp(request), "123");
        boolean isExist = redis.keyIsExist(MOBILE_SMSCODE + ip);
        if (isExist) {
            GraceException.display(ResponseStatusEnum.SYSTEM_SMS_FALLBACK_ERROR);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
